package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Role role, Long[] ids) {
        roleMapper.insert(role);
        //循环遍历id,给角色添加权限
        if (ids != null){
            for (Long id : ids){
                roleMapper.insertRelation(role.getId(),id);
            }
        }
    }

    @Override
    public Role selectById(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void update(Role role, Long[] ids) {
        //先进行修改操作
        roleMapper.updateByPrimaryKey(role);
        //通过角色id删除数据库管理关系
        roleMapper.deleteByRelation(role.getId());
        //循环遍历id,给角色添加权限


    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> list = roleMapper.queryForDate(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Role> selectByRoleRelation(Long id) {
        return roleMapper.selectByRoleRelation(id);
    }
}
